import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    user: "",
    list: [
      {
        name: "六🐛🐑",
        age: "30",
        city: "南阳",
        id: 1,
      },
      {
        name: "迪丽热巴",
        age: "30",
        city: "新疆",
        id: 2,
      },
      {
        name: "北京富婆",
        age: "55",
        city: "北京大兴",
        id: 3,
      },
      {
        name: "金水区富婆",
        age: "65",
        city: "郑州祭城",
        id: 4,
      },
      {
        name: "中牟富婆",
        age: "45",
        city: "郑州九龙镇",
        id: 5,
      },
      {
        name: "积云知识分子",
        age: "18",
        city: "郑州祥瑞街",
        id: 6,
      },
    ],
  },
  mutations: {
    setuser(state, val) {
      state.user = val;
    },
    exit(state) {
      state.user = "";
    },
    add(state, obj) {
      state.list.push(obj);
    },
    remove(state, sel) {
      // state.list.forEach((item, index) => {
      //   if (sel.includes(index)) {
      //     state.list.splice(index, 1);
      //   }
      // });

      // for (var i = 0; i < state.list.length; i++) {
      //   if (sel.includes(state.list[i].id)) {
      //     state.list.splice(i, 1);
      //     i--;
      //   }
      // }
      //循环找到要删的把他删除，但是因为下标会往前挪一位，让i--，保证每个数据都不能逃过循环

      state.list = state.list.filter((item) => {
        // return sel.includes(item.id) == false;
        return sel.indexOf(item.id) == -1;
      });
      // filter是过滤 把满足过滤条件的item放在的一个新数组中
      //把不在要删除的列表中的数据留下来，根据id，如果这个item的id不再sel中，就代表他不是要删除的数据
      // includes 看某个元素在不在数组中，在的话就是true不在就是false
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      localstorage: window.localStorage,
    }).plugin,
  ],
});
